package my.ts.code;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import my.ts.code.listener.ConfirmListener;
import my.ts.code.net.InComingListener;
import my.ts.code.objects.Root;
import my.ts.code.parser.Parser;
import my.ts.code.util.SMSSender;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class BaseActivity extends Activity implements InComingListener {

	ProgressDialog dialog;
	Context myContext;
	ConfirmListener cListener;
	Dialog cDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
	}

	@Override
	public void dispatch(String data) {
		// TODO Auto-generated method stub

	}

	public void showDialodElert(final Context context, final Activity activity,
			String title, String mess, final boolean close) {
		final AlertDialog alertDialog = new AlertDialog.Builder(context)
				.create();
		alertDialog.setTitle(title);
		alertDialog.setMessage(mess);
		alertDialog.setButton("Đóng", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {

				// here you can add functions
				if (close)
					activity.finish();
				alertDialog.dismiss();
			}
		});
		alertDialog.show();

	}

	public void showDialogConfirmExit(final Context context,
			final Activity activity, String title, String message,
			final boolean end) {
		final AlertDialog alertDialog = new AlertDialog.Builder(context)
				.create();
		alertDialog.setTitle(title);
		alertDialog.setMessage(message);
		alertDialog.setButton("Đồng ý", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {

				// here you can add functions
				if (end)
					activity.finish();
				alertDialog.dismiss();
			}
		});
		alertDialog.setButton2("Đóng", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {

				// here you can add functions
				alertDialog.dismiss();
			}
		});
		alertDialog.show();
	}

	public void showDialogConfirmSMS(final Context context,
			final Activity activity, final String mess, final String title,
			final String code, final String number, final boolean gift,
			final boolean charge) {

		cDialog = new Dialog(context);
		cDialog.setContentView(R.layout.dialog);
		cDialog.setTitle(title);
		cDialog.show();

		LinearLayout giftLayout = (LinearLayout) cDialog
				.findViewById(R.id.gift_layout);
		if (!gift)
			giftLayout.setVisibility(View.GONE);

		TextView message = (TextView) cDialog.findViewById(R.id.message);
		if (mess == null || mess.equals(""))
			message.setVisibility(View.GONE);
		else
			message.setText(mess);

		final EditText giftNumber = (EditText) cDialog
				.findViewById(R.id.gift_number);
		final EditText giftText = (EditText) cDialog
				.findViewById(R.id.gift_text);

		Button ok = (Button) cDialog.findViewById(R.id.ok);
		Button cancel = (Button) cDialog.findViewById(R.id.cancel);

		ok.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				cDialog.dismiss();
				if (gift) {
					String phone = giftNumber.getText().toString().trim();
					String text = giftText.getText().toString().trim();

					if (phone.equals(""))
						showDialogConfirmSMS(context, activity, mess, title,
								code, number, gift, charge);
					else {
						System.out.println("NUMBER 2=" + number);
						SMSSender.sendSMS(number, code + " " + phone + " "
								+ text, activity);
						if (charge)
							cListener.confirmListener(true);
					}
				} else {
					SMSSender.sendSMS(number, code, activity);
					if (charge)
						cListener.confirmListener(true);
				}

			}
		});

		cancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				cDialog.dismiss();
				cListener.confirmListener(false);
			}
		});

	}

	public void showDialogSelectSms(final Context context,
			final Activity activity, final String title, final String message,
			final String type, final String code, final String number) {

		AlertDialog ad = new AlertDialog.Builder(context).create();
		ad.setTitle("Tùy chọn");
		// ad.setMessage(mess);
		ad.setButton("Tải về", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				showDialogConfirmSMS(context, activity,
						message.replace("$$$", "tải"), "Tải" + type, code,
						number, false, false);

			}
		});
		ad.setButton3("Tặng bạn bè", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				showDialogConfirmSMS(context, activity,
						message.replace("$$$", "tặng"), "Tặng" + type, code,
						number, true, false);
			}
		});
		ad.setButton2("Đóng", new OnClickListener() {
			public void onClick(DialogInterface dialog, int arg1) {
				// do nothing
			}
		});
		ad.setCancelable(true);
		ad.show();
	}

	public void showDialogLoading(Context context, String title) {
		dialog = new ProgressDialog(context);
		dialog.setTitle(title);
		dialog.show();
	}

	public void showDialogLoading(Context context) {
		dialog = new ProgressDialog(context);
		dialog.show();
	}

	public void hideDialogLoading() {
		if (dialog != null)
			dialog.dismiss();
	}

	public Root parse(String data) {
		Root root = null;

		InputStream is = null;
		try {
			is = new ByteArrayInputStream(data.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		if (is != null) {
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp;
			try {

				sp = spf.newSAXParser();

				XMLReader xr = sp.getXMLReader();
				Parser parser = new Parser();
				xr.setContentHandler(parser);
				xr.parse(new InputSource(is));

				root = parser.getRoot();

				is.close();

			} catch (ParserConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return root;
	}

	public void setCListener(ConfirmListener listener) {
		this.cListener = listener;
	}

	@Override
	public void connectionError(boolean error) {
		// TODO Auto-generated method stub

	}
}
